package pam.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import pam.core.XMLObject;

/**
 * XMLObject容器
 */
public class XMLObjectList implements XMLObject {

	private List list;

	private int page;

	private int size;

	private long total;

	public XMLObjectList() {
		list = new ArrayList();
	}

	public XMLObjectList(List list) {
		this.list = list;
		page = 1;
		if (list != null) {
			size = list.size();
			total = size;
		}
	}

	public XMLObjectList(int page, int size, long total) {
		list = new ArrayList();
		this.page = page;
		this.size = size;
		this.total = total;
	}

	public XMLObjectList(List list, int page, int size, long total) {
		this.list = list;
		this.page = page;
		this.size = size;
		this.total = total;
	}

	public final int getPage() {
		if (page == 0) {
			page = 1;
		}
		return page;
	}

	public final void setPage(int page) {
		this.page = page;
	}

	public final int getSize() {
		if (size == 0) {
			if (list != null) {
				size = list.size();
			}
		}
		return size;
	}

	public final void setSize(int size) {
		this.size = size;
	}

	public final long getTotal() {
		if (total == 0) {
			total = getSize();
		}
		return total;
	}

	public final void setTotal(long total) {
		this.total = total;
	}

	public final void add(XMLObject object) {
		if (list == null) {
			list = new ArrayList();
		}

		list.add(object);
	}

	public final int indexOf(XMLObject object) {
		if (list == null) {
			return -1;
		}

		return list.indexOf(object);
	}

	public XMLObject get(int index) {
		if (list == null) {
			return null;
		}

		return (XMLObject) list.get(index);
	}

	public final Iterator iterator() {
		if (list == null) {
			return null;
		}

		return list.iterator();
	}

	public final boolean contains(XMLObject object) {
		if (list == null) {
			return false;
		}

		return list.contains(object);
	}

	public final void remove(XMLObject object) {
		if (list == null) {
			return;
		}

		list.remove(object);
	}

	public final void addAll(Collection collection) {
		if (list == null) {
			return;
		}

		list.addAll(collection);
	}

	public final void addAll(XMLObjectList collection) {
		if (list == null) {
			return;
		}

		if (collection == null) {
			return;
		}

		Iterator iterator = collection.iterator();
		if (iterator == null) {
			return;
		}

		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
	}

	public final boolean isEmpty() {
		if (list == null) {
			return false;
		}

		return list.isEmpty();
	}

	public final int size() {
		if (list == null) {
			return 0;
		}

		return list.size();
	}

	private long getMaxPage() {
		if (getSize() <= 0) {
			return 0;
		}

		long maxPage = getTotal() / getSize();

		if (getTotal() % getSize() > 0) {
			maxPage++;
		}

		return maxPage;
	}

	public String toXML() {

		if (list == null) {
			return null;
		}

		StringBuffer sb = new StringBuffer();
		sb.append("<List");

		sb.append(" total=\"");
		sb.append(getTotal());
		sb.append("\"");

		sb.append(" page=\"");
		sb.append(getPage());
		sb.append("\"");

		sb.append(" size=\"");
		sb.append(getSize());
		sb.append("\"");

		sb.append(" max-page=\"");
		sb.append(getMaxPage());
		sb.append("\"");

		sb.append(">\n");

		for (Iterator it = list.iterator(); it.hasNext();) {
			sb.append(((XMLObject) it.next()).toXML());
		}

		sb.append("</List>\n");

		return sb.toString();

	}

}
